<?php
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: {creation_date}
 * @project		: 
 * @description	: {description}
 *		- 
 *
\* ************************************************** */
class Phatsinh extends MY_Controller {
    
    function __construct( ) {
        parent::__construct( );
		$this->load->library( array( 'form_validation', 'table' ) );		
		$this->load->helper( array( 'form', 'url' ) );
		$this->load->model( 'CodeGenerator_model', '', TRUE );
		$this->load_template();	
	}	
	
	function index( ){
		$this->manage( );
	}

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: View list
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function view( ){
        // Get rules from config file
        // --------------------------------------------------
	    //$rules                  =   $this->form_validation->_config_rules[ 'phatsinh_add' ];
        $rules                  =   array(array('field_id' => 'PS_ID','label' => 'PS_ID','control' => 'Array',),array('field_id' => 'Content','label' => 'Content','control' => 'Array',),array('field_id' => 'Price','label' => 'Price','control' => 'vnd',),array('field_id' => 'Create_Dt','label' => 'Create_Dt','control' => 'Array',),);
	    $this->data[ 'rules' ]  =   $rules;
       
        // Make header
        // --------------------------------------------------
	    $header = array( );
	    foreach( $rules as $item ){
	        $header[ ] = $item[ 'label' ];
	    }
       
	    $this->data[ 'header' ] =   $header;
	    $results                =   $this->CodeGenerator_model->get( 'phatsinh', 'PS_ID,Content,Price,Create_Dt' );
	
        $actions  = array();
            
        
        
        // Make action Rent/Contact/Review
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
            $id = array_values( $results[ $i ] );
            
            if(!empty($actions)){
                foreach ($actions as $item) {
                    $results[ $i ][ $item['action_name'] ]   = anchor( base_url( ) . 
                    'index.php/phatsinh/'. $item['action_func'] . "/" . $id[ 0 ], $item['action_name']);
                }
            }
            
        }
        
        $this->data[ 'results'        ]   =   $results;
        $this->data[ 'master_limited' ]   =   explode(',', 'PS_ID,Content,Price,Create_Dt');
        $this->data[ 'custom_action'  ]   =   $actions;
 
        $this->load->view( 'phatsinh_list', $this->data ); 
    }
    
    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Manage list
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function manage( ){
        // Get rules from config file
        // --------------------------------------------------
		$this->autoThemPhatSinh();
		$time=$this->input->post('phatsinh_dt');
		
		if(empty($time))
			$time = date("m/Y",time());
		$this->data['time'] = $time;
		
		$limit = $this->getLimit("M","06/".$time);
		
        //$rules                  =   $this->form_validation->_config_rules[ 'phatsinh_add' ];
        $rules                  =         array(array('field_id' => 'PS_ID','label' => 'ID','control' => 'Array', 'class' => 'first border-right', 'style' => 'border-right: solid 3px #fff;',),array('field_id' => 'Content','label' => 'Nội dung','control' => 'Array', 'class' => 'border-right', 'style' => 'border-right: solid 3px #fff;',),array('field_id' => 'Price','label' => 'Tiền','control' => 'vnd', 'class' => 'border-right', 'style' => 'border-right: solid 3px #fff;',),array('field_id' => 'Create_Dt','label' => 'Ngày tạo','control' => 'time', 'class' => 'border-right', 'style' => 'border-right: solid 3px #fff;',),array('field_id' => 'auto','label' => 'Tu dong','control' => 'Array', 'class' => 'border-right', 'style' => 'border-right: solid 3px #fff;',));
        $this->data[ 'rules' ]  =   $rules;
       
        // Make header
        // --------------------------------------------------
        $header = array( );
        foreach( $rules as $item ){
            $header[ ] = $item[ 'label' ];
        }
       
        $this->data[ 'header' ] =   $header;
        $results                =   $this->CodeGenerator_model->get( 'phatsinh', 'PS_ID,Content,Price,Create_Dt,auto' ,'Create_dt >='.$limit['datemin'].' AND Create_dt <= '.$limit['datemax']);
        
        // Make action Edit/Delete
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
	       $id = array_values( $results[ $i ]);
            $results[ $i ][ 'Edit'   ]   =  anchor( base_url( ) . 'index.php/phatsinh/edit/' . $id[ 0 ], 'Edit');  
            $results[ $i ][ 'Delete' ]   =  anchor( 
                        base_url( ) . 'index.php/phatsinh/delete/' . $id[ 0 ], 
                        'Delete',
                        array( 'onClick' => 'return deletechecked(\' ' . base_url() . 'index.php/phatsinh/delete/' . $id[ 0 ] . ' \')' ) );                        
        }
        
        $this->data[ 'results'        ]     =   $results;
        $this->data[ 'master_limited' ]     =   explode(',', 'PS_ID,Content,Price,Create_Dt,auto');
        $this->data[ 'action'         ]     =   array(array('id' => 'new','label' => 'Thêm','url' => 'index.php/phatsinh/add/',),array('id' => 'edit','label' => 'Edit','url' => 'index.php/phatsinh/edit/',),array('id' => 'delete','label' => 'Delete','url' => '',),);
        // $this->load->view( 'phatsinh_list', $this->data ); 
		$this->template->write( 'title', 'Spa - Phát Sinh' );
        $this->template->write_view( 'content', 'phatsinh_list.php',$this->data );
        $this->template->render();
    }
	

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Load form add new record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function add( ){            
		$this->data[ 'custom_error' ] = '';
		$username = "noname";
		$userid = 0;
        
        if( $this->form_validation->run( 'phatsinh_add' ) == false ){
             $this->data[ 'custom_error' ] = ( validation_errors( ) ? '<div class="form_error">' . validation_errors( ) . '</div>' : false );
        }else{  
			$create_dt = set_value('Create_Dt');
			$auto = set_value('auto');
			if(empty($create_dt))$create_dt = time();
			else $create_dt = strtotime(str_replace(array("/"),array("."),$create_dt));		
            $data = array(
                    'Content' => set_value('Content'),
					'Price' => set_value('Price'),
					'auto' => $auto,
					'active' => $auto==1,
					'Create_Dt' => $create_dt
            );
            $result = $this->CodeGenerator_model->add( 'phatsinh', $data, 'PS_ID' );
			if( $result == ADD_SUCCESSFULLY ){
				redirect( base_url() . 'index.php/phatsinh/manage/' );
			}else if( $result == RECORD_EXISTED ){
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>A record existed.</p></div>';
			}else{
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>An Error Occured.</p></div>';
			}
		}		   
		//$this->load->view( 'phatsinh_add', $this->data );
		$this->template->write( 'title', 'Spa - Phát Sinh - Thêm' );
        $this->template->write_view( 'content', 'phatsinh_add.php',$this->data );
        $this->template->render();		
    }	

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Load edit form.
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function edit( ){
        $this->data[ 'custom_error' ] = '';
		$username = "noname";
		$userid = 0;
        
        if( $this->form_validation->run( 'phatsinh_edit' ) == false ){
            $this->data[ 'custom_error' ] = ( validation_errors( ) ? '<div class="form_error">' . validation_errors( ) . '</div>' : false );

        }else{     
			$create_dt = $this->input->post('Create_Dt');
			$auto = $this->input->post('auto');
			if(!empty($create_dt))//$create_dt = time();
			//else 
				$create_dt = strtotime(str_replace(array("/"),array("."),$create_dt));		
            $data = array(
                'Content' => $this->input->post('Content'),
					'Price' => $this->input->post('Price'),
					'auto' => $auto,
					'active' => $auto==1,
					'Create_Dt' => $create_dt
            );
			if(empty($create_dt)) unset($data['Create_dt']);
			if( $this->CodeGenerator_model->edit( 'phatsinh', $data, 'PS_ID', $this->input->post( 'PS_ID' ) ) == TRUE ){
				redirect( base_url( ) . 'index.php/phatsinh/manage/' );
			}else{
				$this->data[ 'custom_error' ] = '<div class="form_error"><p>An Error Occured</p></div>';
			}
		}
		$this->data[ 'result' ]   =   $this->CodeGenerator_model->get( 'phatsinh', 'PS_ID,Content,Price,Create_Dt,auto', 'PS_ID = "' . $this->uri->segment( 3 ) . '"', NULL, NULL, true );  
		//$this->load->view( 'phatsinh_edit', $this->data );	
			$this->template->write( 'title', 'Spa - Phát Sinh - Sửa' );
        $this->template->write_view( 'content', 'phatsinh_edit.php',$this->data );
        $this->template->render();		
    }

    /* ------------------------------------------------------------ *\
     | - FUNC Purpose	: Delete record
     | - Parameters		: 
     |		Name			Type		Description
     |		---------------------------------------------------
     |		N/A
     |								
     | - Return			: 
     |			- N/A 
    \* ------------------------------------------------------------ */
    function delete( ){
        $ID =  $this->uri->segment( 3 );
        $this->CodeGenerator_model->delete(  'phatsinh', 'PS_ID', $ID );             
        redirect( base_url( ) . 'index.php/phatsinh/manage/' );
    }       
}